﻿using System;
using System.Linq;
using RockPaperScissors.DataModel;

namespace RockPaperScissors.Web
{
    public partial class ViewBouts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadResults(SettingsHelper.GetRoundNumber());
        }

        private void LoadResults(int round)
        {
            DataModel.RockPaperScissors context = new DataModel.RockPaperScissors();

            Match match = (from m in context.Matchs
                            where m.Round == round
                            orderby m.MatchId descending
                            select m).FirstOrDefault();

            if (match != null)
            {
                lblTeamOne.Text = match.TeamName1;
                lblTeamTwo.Text = match.TeamName2;

                imgTeamOneMove.ImageUrl = Utility.WebHelper.GetGameImageFromMove(match.Team1Move);
                imgTeamTwoMove.ImageUrl = Utility.WebHelper.GetGameImageFromMove(match.Team2Move);
                lblWinner.Text = match.Winner;

                lblTeamOneRecord.Text = match.Team1Record;
                lblTeamTwoRecord.Text = match.Team2Record;
            }
            else
            {
                pnlBout.Visible = false;
                pnlNoBout.Visible = true;
            }

            if ((match.Winner.ToLower().Contains("wins the round")) || (match.Winner.ToLower().Contains("real rock paper")))
            {
                imgTeamOneMove.Visible = false;
                imgTeamTwoMove.Visible = false;
                lblWins.Visible = false;
            }
            else 
            {
                imgTeamOneMove.Visible = true;
                imgTeamTwoMove.Visible = true;
                lblWins.Visible = true;
            }

            if (match.Winner.ToLower() == "tie")
            {
                 lblWins.Visible = false;
                 imgTeamOneMove.Visible = true;
                 imgTeamTwoMove.Visible = true;
            }
        }
    }
}